package com.framework.modules.sys.shiro;

import org.apache.shiro.codec.Base64;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.context.annotation.Bean;

public class CookieManagerProvider {

    /**
     * cookie保存名称
     */
    public String name;

    /**
     * cookie 过期时间,单：秒
     */
    public int timeout;

    /**
     * 构造函数
     * @param name
     * @param timeout
     */
    public CookieManagerProvider(String name,int timeout){
        this.name = name;
        this.timeout = timeout;
    }

    /**
     * 配置Cookie对象
     * 记住我的cookie：rememberMe
     * @return  SimpleCookie rememberMeCookie
     */
    public SimpleCookie rememberMeCookie() {
        SimpleCookie simpleCookie = new SimpleCookie(this.name);
        simpleCookie.setMaxAge(this.timeout);
        return simpleCookie;
    }

    /**
     * 配置cookie管理对象
     * @return CookieRememberMeManager
     */
    public CookieRememberMeManager cookieRememberMeManager() {
        CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
        cookieRememberMeManager.setCipherKey(Base64.decode("aGVsbG8sY2FjaWE="));
        cookieRememberMeManager.setCookie(this.rememberMeCookie());
        return cookieRememberMeManager;
    }
}
